import { Component, OnDestroy, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {ActivatedRoute, Router} from '@angular/router'
import { Observable } from 'rxjs/internal/Observable';
import { Subscription } from 'rxjs';
@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.css']
})
export class SignupComponent implements OnInit,OnDestroy {
  public err_msg:string = ''
  signForms = {
    email:'',
    password:''
  }
  sub: Subscription ;
  emails:string = ''
  signup(){
    const formData = this.signForms
    console.log('表单提交')
    this.http.post('http://localhost:3000/users',formData).toPromise().then((data:any) => {
      console.log(data)
      window.localStorage.setItem('auth_token',data.token)
      window.localStorage.setItem('user_info',JSON.stringify(data.user))

    this.router.navigate(['contact'])}).catch(e =>{
      console.log(e)
      if(e.status === 409){
        window.alert("邮箱已被占用")

        this.err_msg ="邮箱已被占用"

      }
    })
  }

  constructor(
    private http: HttpClient,
    private router: Router,


    private route: ActivatedRoute
  ) { }

  ngOnInit(): void {
    this.listen() ;
  }

  listen(): void{
    this.sub =  this.route.params.subscribe((params) => {
      const {id , name} = params ;
      console.log(params);

    })
  }
  ngOnDestroy(): void {
    this.sub.unsubscribe() ;
  }
  
}
